
jBGallery.Caption = function(el, data, engine, options){
    var _length = 0;
  var _init = false;
    function _wrap_caption(){
        var _current = engine.current();
        var _html = '', _rel = '';
    var _i = data.get(_current - 1);
    var _rel = _i.title;
        if(_i.rel.length > 0)
        _rel = '<a href="' + _i.rel + '" target="_blank">' + _i.title + '</a>'; 
        if(_i.title.length > 0)
            _html += '<h3>' + _rel + '</h3>';
        if(_i.caption)
            _html += '<div>' + _i.caption + '</div>';
      
    _caption.removeAttr("class").addClass(_i.caption_css).addClass('jbg-caption');
        return _html + '';
    }
    function _toggle(){
    if(_length > 0)
            _caption.toggle();
    }
  function _build(){
    var _html = _wrap_caption();
        _length = _html.length;
        _caption.html(_html);

        if(_length == 0)
            _caption.hide();
  }
    var _caption = $('.jbg-caption',el);
  
  _build();
  
  if(_length > 0)//first
     _caption.fadeIn(options.timers.fade);

    if(options.autohide){
        jBGallery.AutoToggle(_caption, options);
    }
  
    engine.target.bind("load", function(){
        _build();
    });

    
    return {
        toggle : _toggle
    };
};
